package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;

import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;

import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;

import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息为:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功!");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加查询条件 根据name进行like 模糊查询
        lqw.like(name!=null,Setmeal::getName,name);
        //添加排序条件
        lqw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,lqw);
        //分析:上述查询的分页对象中,LIst<Dish> 数据中,每一个Dish对象,没有分页名称
        //创建新的分页对象
        Page<SetmealDto> dtoPage = new Page<>();
        //进行对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");//records属性,分页查询的集合数据,数据是需要处理的 因此我们不拷贝
        //获取基本分页查询的集合数据
        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> dtoList = new ArrayList<>();
        //遍历菜品集合
        records.forEach(setmeal -> {
                    //将setmeal拷贝到setmealdto
                    SetmealDto dto = new SetmealDto();
                    BeanUtils.copyProperties(setmeal, dto);

                    //查询每个菜品所属分类名称
                    Category category = categoryService.getById(setmeal.getCategoryId());
                    dto.setCategoryName(category.getName());

                    //放入集合中
                    dtoList.add(dto);
                });
            dtoPage.setRecords(dtoList);

            //返回分页对象数据
        return R.success(dtoPage);
    }
    //删除数据
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.removeWithDish(ids);

        return R.success("套餐删除成功");
    }

    //启售&禁售
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
        setmealService.updateSetmealStatusById(status,ids);
        return R.success("售卖状态修改成功!");

    }

    //根据id查询
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWith(id);

        return R.success(setmealDto);
    }
    //修改
    @PutMapping
    public R<String> edit(@RequestBody SetmealDto setmealDto){

        if(setmealDto == null){
            return R.error("请求异常");
        }

        if(setmealDto.getSetmealDishes()==null){
            return R.error("套餐没有菜品,请添加套餐");
        }

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Long setmealId = setmealDto.getId();

        LambdaQueryWrapper<SetmealDish> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(queryWrapper);

        //为setmeal_dish表填充相关的属性
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }

        //批量把setmealDish保存到setmeal_dish表
        setmealDishService.saveBatch(setmealDishes);
        setmealService.updateById(setmealDto);

        return R.success("套餐修改成功");
    }

    /**
     * 根据条件查询套餐数据
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
